package com.sense.kukoo.biz_library.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.*

class SubscriptionsViewModel : BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private var mIndex = 2
    private val mPageSize = 20

    private val mChannelData:ArrayList<LibrarySubscriptionBean> = arrayListOf()
    private val mRecentData:ArrayList<BookItemBean> = arrayListOf()

    val action_toDetailPage:MutableLiveData<LibrarySubscriptionBean> = MutableLiveData()

    val enableLoadMore: MutableLiveData<Boolean> = MutableLiveData(true)
    val mSubscriptionsData: MutableLiveData<ArrayList<SubscriptionsBean>> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun refreshChannelData(){
        request {
            val result = Repo.http.subscriptionQueryPage(1, mPageSize)
            if (result.success) {
                //
                val data: QueryPageBean<LibrarySubscriptionBean> = result.data!!
                val records:List<LibrarySubscriptionBean> = data.records
                mChannelData.clear()
                mChannelData.addAll(records)
                showData()
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun refreshData() {
        refreshChannelData()
        request {
            val result = Repo.http.subscriptionQueryItemPage(1, mPageSize)
            if (result.success) {
                val data: QueryPageBean<BookItemBean> = result.data!!
                val records:List<BookItemBean> = data.records
                //
                mIndex=2
                if(data.last){
                    enableLoadMore.postValue(false)
                }else{
                    enableLoadMore.postValue(true)
                }
                mRecentData.clear()
                mRecentData.addAll(records)
                showData()
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun loadMoreData() {
        request {
            val result = Repo.http.subscriptionQueryItemPage(mIndex, mPageSize)
            if (result.success) {
                val data: QueryPageBean<BookItemBean> = result.data!!
                val records:List<BookItemBean> = data.records
                //
                mIndex++
                if(data.last){
                    enableLoadMore.postValue(false)
                }
                //添加并更新数据
                mRecentData.addAll(records)
                showData()
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    private fun showData(){
        val show:ArrayList<SubscriptionsBean> = arrayListOf()
        if(mChannelData.size>0){
            show.add(SubscriptionsBean(SubscriptionsBean.TYPE_CHANNEL, mChannelData, null))
        }
        if(mRecentData.size>0){
            show.add(SubscriptionsBean(SubscriptionsBean.TYPE_RECENTLY_UPDATED_TITLE,null,null))
            mRecentData.forEach {
                show.add(SubscriptionsBean(SubscriptionsBean.TYPE_RECENTLY_UPDATED_ITEM, null, it))
            }
        }
        mSubscriptionsData.postValue(show)
    }

    fun loadItemDataAndPlay(item:BookItemBean){
        request {
            val result = Repo.http.bookDetails("" + item.bookId)
            if (result.success) {
                /*val bookDetailsBean : BookDetailsBean = result.data!!
                //
                val data = LocalMediaListItemBean()
                data.book = item
                data.mCoverUrl = bookDetailsBean.coverUrl
                data.name = bookDetailsBean.name
                data.isSubscription = bookDetailsBean.isSubscription
                data.chapterTotal = bookDetailsBean.chapterTotal
                data.isChapter = bookDetailsBean.isChapter
                data.type = bookDetailsBean.type ?: 0
                //播放
                withContext(Dispatchers.Main){
                    Kukoo.input.play(data)
                }*/
            }
        }
    }

    fun onChannelItemClick(channelItem: LibrarySubscriptionBean){
        toView(ACTION_SHOW_DIALOG)
        request {
            //
            val id = channelItem.id.toString()
            val result = Repo.http.lightOff(id)
            if (result.success){
                action_toDetailPage.postValue(channelItem)
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }
}